package com.budayi.common.exceptionHandler;

import com.budayi.common.exception.BlException;
import com.budayi.vo.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;

/**
 * @author LIBOFEI
 * @ClassName ExceptionHandler
 * @description 捕获异常控制类
 * @date 2021/11/04 11:33
 * @Version 1.0
 **/
@RestControllerAdvice
public class BlExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(BlExceptionHandler.class);


    @ExceptionHandler({IllegalArgumentException.class})
    public JsonResult handleException(HttpServletRequest request, IllegalArgumentException e){
        logger.error("出现异常======> [{参数有误}] ",e);
        String message = e.getMessage();
        return JsonResult.failure(message);
    }

    @ExceptionHandler({MethodArgumentNotValidException.class})
    public JsonResult handleException(HttpServletRequest request, MethodArgumentNotValidException e){
        logger.error("出现异常======> [{参数有误}] ",e);
        String message = e.getBindingResult().getAllErrors().stream().findFirst().get().getDefaultMessage();
        return JsonResult.failure(message);
    }


    @ExceptionHandler({BlException.class})
    public JsonResult handleException(HttpServletRequest request, BlException e){
        return JsonResult.failure(e.getMessage());
    }


    @ExceptionHandler({Exception.class})
    public JsonResult handleException(HttpServletRequest request, Exception e){
        logger.error("出现异常======> [{程序异常}] ",e);
        return JsonResult.failure("系统繁忙");
    }
}

